package com.unam.agenda;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	EditText e_nombre;
	EditText e_tel;
	
	static DatabaseAdapter db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button insertar=(Button) findViewById(R.id.btnAnadir);
		Button ver=(Button) findViewById(R.id.btnVer);
		
		e_nombre=(EditText) findViewById(R.id.edtnombre);
		e_tel=(EditText) findViewById(R.id.edtTelefono);
		
		db=new DatabaseAdapter(this);
		
		insertar.setOnClickListener(this);
		ver.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		
		switch (arg0.getId()) {
		case R.id.btnAnadir:
			inserta();
			break;
		case R.id.btnVer:
			
			Intent i=new Intent(this,ListaContactos.class);
			startActivity(i);
			break;

		default:
			break;
		}
		// TODO Auto-generated method stub
		
	}

	private void inserta() {
		// TODO Auto-generated method stub
		
		String nombre=e_nombre.getText().toString();
		String tel=e_tel.getText().toString();
		db.open();
		
		if(db !=null){
			if(db.creaRegistro(nombre, tel)!=-1)
				Toast.makeText(getApplicationContext(), "Contacto guardado", Toast.LENGTH_LONG).show();
			
			else
				Toast.makeText(getApplicationContext(), "Ha ocurrido un error", Toast.LENGTH_LONG).show();
		}
		db.close();
	}

}
